class Login{
    constructor(){
        //获取节点
        this.text = document.querySelector('input[name="name"]')
        this.pass = document.querySelector('input[name="pass"]')
        this.btn = document.querySelectorAll('.but')
        //初始化
      this.init()
    }
    init(){
     this.Obj()
     this.register()
    }
    Obj(){
      let obj = {}
      this.click(obj)
    }
    click(obj){
     this.btn[0].onclick =(e)=>{
        //阻止默认事件
        e.preventDefault()
        //获取value值
        let text_val = this.text.value
        let pass_val = this.pass.value
        //获取 localStorage的数据
        let key = JSON.parse(localStorage.getItem('username'))
        console.log(key);
        //找到账号相同的这条数据
        let res = key.some(item=>{
        return item.name == text_val && item.pass == pass_val
        })
        //如果账号密码相同登录成功
        if(res){
          //把数据存储到localStorage里面
          obj.name = text_val
          localStorage.setItem('userkey',JSON.stringify(obj))
          //跳转
         location.href = '/index.html'
           }else{
             alert('账号或密码错误')
           }
        }
     }

     register(){
       this.btn[1].onclick = (e)=>{
          //阻止默认事件
        e.preventDefault()
         location.href = '/html/register.html'
       }
     }
    }

new Login()